package cn.bdqn.hospital.dao.impl;


import cn.bdqn.hospital.dao.BlDao;
import cn.bdqn.hospital.entity.Bl;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 病历刀实现类
 */
public class BlDaoImpl implements BlDao {

    QueryRunner queryRunner = new QueryRunner();

    /**
     * 查询所有病历
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Bl> getAll() throws SQLException {
        List<Bl> blList = new ArrayList<Bl>();
        try{
            String sql = "select * from bl;";
            blList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Bl>(Bl.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return blList;
    }

    /**
     * 通过病历id获取信息
     *
     * @param blId
     * @return
     * @throws SQLException
     */
    @Override
    public Bl getBlByBlid(Integer blId) throws SQLException {
        Bl bl = null;
        try{
            String sql = "select * from bl where blId = '?'";
            bl = (Bl) queryRunner.query(sql,new BeanListHandler<Bl>(Bl.class),blId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bl;
    }

    /**
     * 新增病历
     *
     * @param bl
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Bl bl) throws SQLException {
        String sql = "insert into bl(orderId, BLHistory, jcId, blDiagnose, blTherapy, cfId, blAdvice, blDate) values (?,?,?,?,?,?,?,?);";
        Object[] Bl = new Object[]{bl.getOrderId(),bl.getBlHistory(),bl.getJcId(),bl.getBlDiagnose(),bl.getBlTherapy(),bl.getCfId(),bl.getBlAdvice(),bl.getBlDate()};
        return queryRunner.update(DruidUtils.getConnection(), sql, Bl);
    }

    /**
     * 修改病历内容
     *
     * @param bl
     * @return
     * @throws SQLException
     */
    @Override
    public int update(Bl bl) throws SQLException {
        int num = 0;
        String sql = "update Bl set blHistory = ? where blId = ?;";
        num = queryRunner.update(DruidUtils.getConnection(),sql,bl);
        return num;
    }

    /**
     * 删除主题
     *
     * @param blId
     * @return
     * @throws SQLException
     */
    @Override
    public int delBlByBlid(Integer blId) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from Bl where blId='?';";
            num = queryRunner.update(DruidUtils.getConnection(),sql,blId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过检查号查询病历
     *
     * @param jcId
     * @return
     * @throws SQLException
     */
    @Override
    public Bl getBlByJcid(Integer jcId) throws SQLException {
        Bl bl = null;
        String sql = "select * from bl where jcId;";
        bl = (Bl) queryRunner.query(sql,new BeanListHandler<Bl>(Bl.class),jcId);
        return bl;
    }

    /**
     * 通过处方号查询病历
     *
     * @param cfId
     * @return
     * @throws SQLException
     */
    @Override
    public Bl getBlByCfid(Integer cfId) throws SQLException {
        Bl bl = null;
        String sql = "select * from bl where cfId;";
        bl = (Bl) queryRunner.query(sql, new BeanListHandler<Bl>(Bl.class), cfId);
        return bl;
    }

    /**
     * 通过患者号查询病历
     *
     * @param hzId
     * @return
     * @throws SQLException
     */
    @Override
    public Bl getBlByHzid(String hzId) throws SQLException {
        Bl bl = null;
        String sql = "select * from bl where hzId";
        bl = (Bl) queryRunner.query(sql, new BeanListHandler<Bl>(Bl.class), hzId);
        return bl;
    }
}
